package com.superhelper.superhelper_web.controller;

import javax.servlet.ServletException;
import javax.servlet.http.*;

import java.awt.*;
import java.awt.image.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.*;

import javax.imageio.*;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.util.CharArrayBuffer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.superhelper.common.service.RedisService;
import com.superhelper.superhelper_web.common.Consts;
import com.superhelper.superhelper_web.service.ShopService;


/**
 * 图形验证码
 * 
 * @author Elson
 */
@Controller
public class VerifyController {
	protected Logger log = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private ShopService shopService;
	@Autowired
	private RedisService redisService;

	/** 定义图形验证码中绘制字符的字体 **/
	private final Font myFoot = new Font("Arial Black", Font.PLAIN, 18);
	/** 定义验证码的大小 **/
	private final int IMG_WIDTH = 55;
	private final int IMG_HEIGHT = 20;

	/** 定义一个随机得到颜色的方法 **/
	private Color getRandColor(int endc, int beginc) {
		Random random = new Random();
		endc = (endc > 255) ? 255 : endc;
		beginc = (beginc > 255) ? 255 : beginc;
		/** 设置颜色的RGB值 **/
		int r = endc + random.nextInt(beginc - endc);
		int g = endc + random.nextInt(beginc - endc);
		int b = endc + random.nextInt(beginc - endc);
		/** 返回随机颜色 **/
		return new Color(r, g, b);
	}

	/**
	 * 请求验证码
	 * 
	 * @param request
	 * @param response
	 *            　输出图片流
	 * @throws Exception
	 */
	@RequestMapping(value = "verify.do")
	public void execute(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		/** 设置禁示缓存 **/
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		response.setContentType("image/jpeg; charset=GBK");
		/** 创建图象缓存对象 **/
		BufferedImage image = new BufferedImage(IMG_WIDTH, IMG_HEIGHT,
				BufferedImage.TYPE_INT_RGB);
		/** 得到画笔对象 **/
		Graphics g = image.getGraphics();
		Random random = new Random();
		g.setColor(getRandColor(200, 250));
		/** 填充背景颜色 **/
		g.fillRect(1, 1, IMG_WIDTH - 1, IMG_HEIGHT - 1);
		/** 为图形验证码绘制边框 **/
		g.setColor(getRandColor(180, 200));
		g.drawRect(0, 0, IMG_WIDTH - 1, IMG_HEIGHT - 1);
		/** 生成随机干扰线 **/
		g.setColor(getRandColor(160, 200));
		for (int i = 0; i < 80; i++) {
			int x = random.nextInt(IMG_WIDTH - 1);
			int y = random.nextInt(IMG_HEIGHT - 1);
			int x1 = random.nextInt(6) + 1;
			int y1 = random.nextInt(12) + 1;
			g.drawLine(x, y, x + x1, y + y1);
		}
		/** 生成随机干扰线 **/
		g.setColor(getRandColor(160, 200));
		for (int i = 0; i < 80; i++) {
			int x = random.nextInt(IMG_WIDTH - 1);
			int y = random.nextInt(IMG_HEIGHT - 1);
			int x1 = random.nextInt(12) + 1;
			int y1 = random.nextInt(6) + 1;
			g.drawLine(x, y, x - x1, y - y1);
		}
		/** 设置绘制字符的字体 **/
		g.setFont(myFoot);
		/** 用来保存随机生成的字符 **/
		String result = "";
		for (int i = 0; i < 4; i++) {
			String temp = getRandString();
			result += temp;
			/** 获取随机颜色 **/
			g.setColor(new Color(20 + random.nextInt(110), 20 + random
					.nextInt(110), 20 + random.nextInt(110)));
			g.drawString(temp, 12 * i + 2, 16);
		}
		/** 获取HttpSession **/
		HttpSession session = request.getSession(true);
		/** 把随机字符串放入session中等待与用户提交的进行校验 **/
		session.setAttribute(Consts.VERIFY_CODE, result);
		g.dispose();
		/** 向输出流中输出图片 **/
		ImageIO.write(image, "JPEG", response.getOutputStream());
	}

	/** 定义获得随机字符串的方法 **/
	private String getRandString() {
		/** 定义生成一个0,1,2的随机数字 **/
		int rand = (int) Math.round(Math.random() * 2);
		long temp = 0;
		switch (rand) {
		/** 生成大写字母: 暂去除 **/
		case 3:
			temp = Math.round(Math.random() * 25 + 65);
			return String.valueOf((char) temp);
			/** 生成小写字母 **/
		case 4:
			temp = Math.round(Math.random() * 25 + 97);
			return String.valueOf((char) temp);
			/** 生成数字 **/
		default:
			temp = Math.round(Math.random() * 9);
			return String.valueOf(temp);
		}
	}

	/**
	 * 授权回调地址
	 * 
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("api/eleme/auth")
	public void auth(HttpServletRequest request, HttpServletResponse response) {
		String cxt = request.getContextPath();
		String location = cxt + "/api/auth/error.do";
		String state = request.getParameter("state");
		String code = request.getParameter("code");
		log.info("[eleme]auth callback:{}", request.getQueryString());
		try {
			boolean res = shopService.elemeAuthCallBack(
					Integer.parseInt(state), code);
			log.info("[eleme]auth res:{}", res);
			if (res) {
				location = cxt + "/admin/index.do";
			}
			response.sendRedirect(location);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.error("[auth]error:{}", e);
		}
	}

	/**
	 * 授权回调错误地址
	 * 
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("api/auth/error")
	public String error(HttpServletRequest request, HttpServletResponse response) {
		return "auth_error";
	}

	/**
	 * 授权回调地址
	 * 
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("api/mt/auth")
	public void mt(HttpServletRequest request, HttpServletResponse response)
			throws ServletException {
		try {

			InputStream instream = request.getInputStream();
			int i = request.getContentLength();
			if (i < 0) {
				i = 4096;
			}
			Reader reader = new InputStreamReader(instream, "utf-8");
			CharArrayBuffer buffer = new CharArrayBuffer(i);
			char[] tmp = new char[1024];
			int l;
			while ((l = reader.read(tmp)) != -1) {
				buffer.append(tmp, 0, l);
			}
			String queryBody = buffer.toString();
			log.info("[mt]POST auth callback{}", queryBody);
			String auserId = request.getParameter("auserId");
			log.info("[mt]POST auth auserId:{}", auserId);
			if (queryBody.contains("appAuthToken")) {
				String params[] = queryBody.split("&");
				String shopId = null;
				String appAuthToken = null;
				for (String s : params) {
					if (s.contains("ePoiId=")) {
						shopId = s.replace("ePoiId=", "");
					} else if (s.contains("appAuthToken=")) {
						appAuthToken = s.replace("appAuthToken=", "");
					}
				}
				if (StringUtils.isNotBlank(shopId)) {
					boolean b = shopService.mtAuthCallBack(
							Integer.parseInt(auserId), shopId, appAuthToken);
					if (b) {
						response.getOutputStream().write(
								"{\"data\":\"success\"}".getBytes("UTF-8"));
					}
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}